﻿using System;
using System.Windows;
using System.Windows.Data;

namespace Wp7Tools {
    public class DataContextProxy : FrameworkElement {
        public string BindingPropertyName { get; set; }
        public BindingMode BindingMode { get; set; }

        public DataContextProxy() {
            //Debugger.Break();
            Loaded += DataContextProxyLoaded;
        }

        private void DataContextProxyLoaded(object sender, RoutedEventArgs e) {
            Binding binding = new Binding();
            if (!String.IsNullOrEmpty(BindingPropertyName)) {
                binding.Path = new PropertyPath(BindingPropertyName);
            }
            binding.Source = DataContext;
            binding.Mode = BindingMode;
            SetBinding(DataSourceProperty, binding);
        }

        public Object DataSource {
            get { return GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof (Object), typeof (DataContextProxy), null);
    }
}